<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addGradeOneDept"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="departs" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleCommand">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          :command="{ type: 'add', data: data }"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          :command="{ type: 'edit', id: data.id }"
                        >修改部门</el-dropdown-item>
                        <!-- @click.native 绑定事件 -->
                        <!-- <el-dropdown-item @click.native="del">删除部门</el-dropdown-item> -->
                        <el-dropdown-item
                          :command="{ type: 'del', id: data.id }"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <addDept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        :departs-list="departsList"
        @addSuccess="getDepartmentList"
      />
    </div>
  </div>
</template>

<script>
import { getDepartmentListApi, delDepartmentApi } from '@/api/departments'
import addDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      company: {
        name: ''
      },
      departs: [],
      // 放组织架构中处理之前所有的数据
      departsList: [],
      showDialog: false,
      // 表示当前点击这一行的数据
      nodeData: {},
      isLoading: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    /**
     * 公司点击添加的时候（添加的是一级部门）
     */
    addGradeOneDept() {
      this.showDialog = true
      this.nodeData = this.company
      this.$refs.addDept.getDepartmentManagerList()
    },
    handleCommand({ type, id, data }) {
      if (type === 'add') {
        // console.log('点击了添加')
        this.openDialog(data)
      }
      if (type === 'edit') {
        // console.log('点击了修改')
        this.editDepartment(id)
      }
      if (type === 'del') {
        this.delDepartment(id)
      }
    },
    /**
     * 编辑
     */
    editDepartment(id) {
      this.showDialog = true
      // this.$refs.addDept.title = '修改部门'
      this.$refs.addDept.getDepartmentDetail(id)
    },
    /**
     * 添加部门
     */
    openDialog(data) {
      this.showDialog = true
      // console.log(data)
      this.nodeData = data
      // 调用子组件中的方法
      // this.$refs.form.validate
      // console.log(this.$refs.addDept)
      this.$refs.addDept.title = '新增部门'
      // this.$refs.addDept.getDepartmentManagerList()
    },
    /**
     *关闭弹框
     */
    closeDialog() {
      this.showDialog = false
    },
    /**
     * 删除部门
     * @param {*} id
     */
    delDepartment(id) {
      // console.log(id)
      this.$confirm('确定要删除此数据吗？', '温馨提示')
        .then(async() => {
          // 点击了确定
          await delDepartmentApi(id)
          this.$message.success('删除成功')
          this.getDepartmentList()
        })
        .catch(() => {
          // 点击了取消
        })
    },
    /**
     * 获取组织架构列表
     */
    async getDepartmentList() {
      this.isLoading = true
      const {list} = await getDepartmentListApi()
      console.log('list----',list);
      this.departs = list
      // this.departsList = JSON.parse(JSON.stringify(res.data.depts))
      // this.departs = res.list
      // // console.log(this.departs)
      // this.company.name = res.data.companyName
      // this.company.children = this.departs
      // this.company.id = ''
      this.isLoading = false
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url('~@/assets/common/add-circle.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url('~@/assets/common/minus-circle.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url('~@/assets/common/user-filling.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
